<?php
namespace app\index\controller;
use app\index\model\Feedback;
use app\index\model\Teacher;
use app\index\model\User;
use think\Controller;
use app\index\model\User as UserModel;
use app\admin\model\Notice as NoticeModel;
use think\facade\Session;
use think\Validate;
use think\Db;
use app\index\validate\Regist as RegistVa;
use app\index\validate\Login as LoginVa;
class Index extends Controller
{
    protected $middleware = [
        'UserCheck' => ['except' => ['regist','login','index','email_send'] ]
    ];
    //首页
    public function index()
    {
        return $this->fetch();
    }
    //用户登录
    public function login(){
        if ($this->request->isAjax()){
            $data=input('post.');
            $validate=new LoginVa();
            if ($validate->check($data)==true) {
                
                    $user=new UserModel();
                    $user=$user->where(
                    ['username'=>$data['username'],
                    'password'=>md5($data['password'])]
                    )->find();
                    if ($user){
                        $msg=$this->msg(200,'登录成功！正在跳转...');
                        Session::set('user',$user);
                        }else{
                        $msg=$this->msg(400,'账号或密码错误！');
                        }
                    
            }else{
                $msg=$this->msg(400,$validate->getError());
            }
            return $msg;
        }
        return $this->fetch();
    }
    //用户注册
    public function regist(){
        if ($this->request->isAjax()){
            $data=input('post.');
            $validate=new RegistVa();//实例化注册验证类
            if ($validate->check($data)==true) {//调用验证类的check方法
                if ($data['yzm']!=Session::get('yzm')) {//验证邮箱验证码
                    $msg= $this->msg(400,'邮箱验证码不正确');
                }else{
                    Session::delete('yzm');
                    $add=new UserModel();
                    $add=$add->user_add($data);
                    if ($add) {
                        $msg=$this->msg(200,'注册成功');                    
                    }else{
                        $msg=$this->msg(400,'注册失败'); 
                    }
                }
            }else{
                $msg= $this->msg(400,$validate->getError());
            }
            return $msg;
        }
        return $this->fetch();
    }
    //用户反馈模块
    public function feedback(){
        if ($this->request->isPost()){
            $content=input('post.content');
            $feedback=new Feedback();
            $feedback=$feedback->save(
                [
                    'content'=>$content,
                    'user_id'=>Session::get('user')['id']
                ]
            );
            if ($feedback){
                $msg=$this->msg(200,'提交成功，请等待管理员查询。');
            }else{
                $msg=$this->msg(400,'提交失败，请重新提交。');
            }
            return $msg;
        }
        return $this->fetch();
    }
    //关于我的
    public function about_me(){
        $user_id=\session('user')['id'];
        $feedback=new Feedback();
        $feedbacks=$feedback
            ->where('user_id',$user_id)
            ->order('update_time','desc')
            ->paginate(10);
        $this->assign('feedbacks',$feedbacks);
        return $this->fetch();
    }
    //修改密码
    public function update_password(){
        $data=input('post.');
        $validate=new Validate();
        $rule=[
            'oldpassword|原密码'=>'require|length:6,12',
            'newpassword|新密码'=>'require|length:6,12',
            'newpassword1|确认密码'=>'require|confirm:newpassword',
        ];
        $va=$validate->check($data,$rule);
        if ($va){
            if ((md5($data['oldpassword'])==\session('user')['password'])==true){
                $user=new User();
                $user=$user->save([
                    'password' =>md5($data['newpassword'])
                ],['id'=>\session('user')['id']]);
                if ($user){
                    $msg=$this->msg(200,'密码修改成功');
                }else{
                    $msg=$this->msg(400,'密码修改失败');
                }
            }else{
                $msg=$this->msg(400,'原密码不正确');
            }
        }else{
            $msg=$this->msg(400,$validate->getError());
        }
        return $msg;
    }
    //修改个人信息
    public function update_message(){
        $data=input('post.');
        $rule=[
          'realname|真实姓名'=>'require',
          'tel|手机号码'=>'require',
          'email|邮箱'=>'require|email',
        ];
        $validate=new Validate();
        $va=$validate->check($data,$rule);
        if ($va==true){
            $update=Db::table('user_info')
                ->where('user_id',Session::get('user')['id'])
                ->update([
                    'realname'=>$data['realname'],
                    'tel'     =>$data['tel'],
                    'email'   =>$data['email']
                ]);
            if ($update){
                $msg=$this->msg('200','修改成功');
                $message=UserModel::get(Session::get('user')['id']);
                Session::set('user',$message);
            }else{
                $msg=$this->msg('400','修改失败');
            }
        }else{
            $msg=$this->msg('400',$validate->getError());
        }
        return $msg;
    }
    //上传头像
    public function upload_icon(){
        $icon=request()->file('file');
        $info=$icon->move('../public/uploads');
        if ($info){
            $user=new User();
            $change=$user->save([
             'img'=>$info->getSavename()
            ],['id'=>\session('user')['id']]);
            if ($change){
                $msg=[
                    'statu'=>200,
                    'msg'  =>'上传成功',
                    'path' =>$info->getSavename()
                ];
                Session::set('user.img',$info->getSavename());
            }else{
                $msg=$this->msg(400,'上传失败');
            }
        }else{
           $msg=$this->msg(400,'上传失败');
        }
        return $msg;
    }
    //公告消息 
    public function notice(){
        $notice=new NoticeModel();
        $notice=$notice->order('create_time','desc')->paginate(5);
        $this->assign('notices',$notice);
        return $this->fetch();
    }
    //公告内容
    public function notice_info($id){
        $notice=NoticeModel::get($id);
        $this->assign('notice',$notice);
        return $this->fetch();
    }
    //教师信息
    public function teacher_list(){
        $teacher=new Teacher();
        $teachers=$teacher->paginate(12);
        $this->assign('teachers',$teachers);
        return $this->fetch();
    }
    //发送邮箱验证码
    public function email_send(){
        $toemail=input('post.email');
        $yzm=rand(10000,99999);
        $title="实验室管理系统-验证码";
        $body="您的验证码是:$yzm";
        $mail=new Mail();
        $mail=$mail->send_mail($toemail,$title,$body);
        if ($mail===true){
            Session::set('yzm',$yzm);
            $msg=$this->msg(200,'邮件发送成功');
        }else{
            $msg=$this->msg(400,$mail);
        }
        return $msg;
    }
    //信息
    private function msg($statu,$msg){
        return [
            'statu'=>$statu,
            'msg'  =>$msg
        ];
    }
    //注销
    public function logout(){
        Session::delete('user');
        $this->success('注销成功','/');
    }
}
